package com.hr.dao;

import com.hr.model.Department;
import com.hr.model.Evaluate;
import org.apache.ibatis.annotations.*;
import org.springframework.stereotype.Repository;

import java.util.List;

@Repository
public interface EvaluateDAO
{
    @Results(id="evaluate",value = {
            @Result(id = true, property = "id", column = "e_id"),
            @Result(property = "role", column = "e_role"),
            @Result(property = "content", column = "e_content"),
            @Result(property = "grade", column = "e_grade"),
            @Result(property = "date", column = "e_date"),

            @Result(property = "commenter", column = "u_id", one=@One(select = "com.hr.dao.UserDAO.getUserByUId"))
    })
    @Select("select * from evaluation where e_id=#{eid}")
    Evaluate getEvaluateByEId(@Param("eid")int eid);

    @ResultMap("evaluate")
    @Select("select * from evaluation where a_id=#{aid}")
    List<Evaluate> getEvaluatesByAId(@Param("aid")int aid);

    @SelectKey(keyProperty = "evaluate.id",before = false,resultType = Integer.class,statement = "select last_insert_id()")
    @Insert("insert into evaluation (a_id, u_id, e_role, e_content, e_grade, e_date) " +
            "VALUES (#{aid},#{evaluate.commenter.id},#{evaluate.role},#{evaluate.content},#{evaluate.grade},#{evaluate.date})")
    void insertEvaluation(@Param("aid")int aid,@Param("evaluate")Evaluate evaluate);

    @Update("update evaluation set e_role=#{evaluate.role}, e_content=#{evaluate.content}, e_grade=#{evaluate.grade}, e_date=#{evaluate.date} " +
            "where e_id = #{evaluate.id}")
    void updateEvaluation(@Param("evaluate")Evaluate evaluate);

    @Delete("delete from evaluation where e_id=#{eid}")
    void deleteEvaluation(@Param("eid")int eid);
}
